﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace KM.Framework.Utility
{
   public class Utility
    {
        public static readonly object obj=new object();
        public static string WebPost(string url, string data)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "application/json";
                if (!data.IsNullOrEmptyOrWhiteSpace())
                {
                    string strContent = data;
                    using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
                    {
                        dataStream.Write(strContent);
                        dataStream.Close();
                    }
                }
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string encoding = response.ContentEncoding;
                if (encoding == null || encoding.Length < 1)
                {
                    encoding = "UTF-8"; //默认编码  
                }
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
                string retString = reader.ReadToEnd();
                return retString;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public static void Log(string logTxt,string direType= "RecordLogs", string btype = "KMElectronicScale")
        {
            try
            {
                lock (obj) 
                {
                    DateTime cDate = DateTime.Now;
                    string path = $@"./Logs/{direType}/{cDate.Year}-{cDate.Month}-{cDate.Day}";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    path = path + "/log.txt";
                    if (!File.Exists(path))
                    {
                        File.Create(path).Close();
                    }
                    using (StreamWriter sw = File.AppendText(path))
                    {
                        sw.WriteLine("");
                        sw.WriteLine($@"时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.sss")} 【{btype}】");
                        sw.WriteLine(logTxt);
                        sw.Flush();
                        sw.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        //public static void LYLog(string logTxt,string btype= "KMElectronicScale")
        //{

        //    try
        //    {
        //        DateTime cDate = DateTime.Now;
        //        string path = $@"./Logs/LYLogs/{cDate.Year}-{cDate.Month}-{cDate.Day}";
        //        if (!Directory.Exists(path))
        //        {
        //            Directory.CreateDirectory(path);
        //        }
        //        path = path + "/log.txt";
        //        if (!File.Exists(path))
        //        {
        //            File.Create(path).Close();
        //        }
        //        using (StreamWriter sw = File.AppendText(path))
        //        {
        //            sw.WriteLine("");
        //            sw.WriteLine($@"时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.sss")} 【{btype}】");
        //            sw.WriteLine(logTxt);
        //            sw.Flush();
        //            sw.Dispose();
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}
    }


}
